package com.star.vrserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.star.vrserver.api.R;
import com.star.vrserver.entity.Vrorder;
import com.star.vrserver.entity.query.VrorderQuery;
import com.star.vrserver.service.VrorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author star
 * @since 2023-03-09
 */
@Api(tags = "预约订单信息管理Api")
@RestController
@RequestMapping("/vrorder")
public class VrorderController {
    @Autowired
    private VrorderService vrorderService;

    //获取所有疫苗预约订单信息
    @ApiOperation(value = "获取所有疫苗预约订单信息")
    @GetMapping("getAllVrorders")
    public R getVrorders() {
        return R.ok().data("VrorderLists", vrorderService.list());
    }

    //根据id查询疫苗预约订单信息
    @ApiOperation(value = "根据id查询疫苗预约订单信息")
    @GetMapping("getVrorder")
    public R getVrorder(@RequestParam String id) {
        return R.ok().data("vrorderInfo", vrorderService.getById(id));
    }

    //根据id删除疫苗预约订单
    @ApiOperation(value = "根据id删除疫苗预约订单")
    @DeleteMapping("deleteVrorder/{id}")
    public R deleteVrorder(@PathVariable String id) {
        //是否删除成功：逻辑删除
        boolean flag = vrorderService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    //添加疫苗预约订单
    @ApiOperation(value = "添加疫苗预约订单")
    @PostMapping("saveVrorder")
    public R saveVrorder(@RequestBody Vrorder vrorder) {
        boolean flag = vrorderService.save(vrorder);
        return flag ? R.ok() : R.error();
    }

    //分页查询疫苗预约订单  current 当前页  limit 每页记录数
    @ApiOperation(value = "分页展示疫苗预约订单列表")
    @GetMapping("pageVrorder/{current}/{limit}")
    public R pageListVrorder(@PathVariable long current,
                                 @PathVariable long limit) {
        //创建page对象
        Page<Vrorder> vrorderPage = new Page<>(current, limit);
        //调用方法:底层封装，把分页所有数据封装到pageVrorder对象里面
        vrorderService.page(vrorderPage);
        //获取总记录数
        long total = vrorderPage.getTotal();
        //获取最终分页结果集
        List<Vrorder> records = vrorderPage.getRecords();
        //将结果组装返回前端
        return R.ok().data("total", total).data("records", records);
    }

    //条件查询带分页的方法
    @ApiOperation(value = "分页展示疫苗预约订单列表(带条件)")
    @PostMapping("pageVrorderCondition/{current}/{limit}")
    public R pageListVrorder(@PathVariable long current, @PathVariable long limit,
                                 @RequestBody(required = false) VrorderQuery vrorderQuery) {
        return R.ok();
    }
    //修改疫苗预约订单信息
    @ApiOperation(value = "修改疫苗预约订单信息")
    @PostMapping("updateVrorder")
    public R updateVrorder(@RequestBody Vrorder vrorder) {
        boolean flag = vrorderService.updateById(vrorder);
        return flag ? R.ok() : R.error();
    }


}
